package actions;

import models.StoreModel;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import service.StoreService;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
public class StoreAction extends CoreAction implements ModelDriven<StoreModel> {
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(StoreAction.class);
	private StoreService storeService;
	private StoreModel model = new StoreModel();

	public void setModel(StoreModel model) {
		LOGGER.info("Start method setModel, params: " + model);
		this.model = model;
	}

	@Autowired
	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	@Action(value = "store", results = { @Result(name = "success", type = "json") })
	public String execute() {
		return SUCCESS;
	}

	@Action(value = "getStores", results = { @Result(name = "success", type = "json") })
	public String getAll() {
		LOGGER.info(" Start action - 'getStores'");
		model.setList(storeService.getAll());
		LOGGER.info(" End action - 'getStores'");
		return SUCCESS;
	}

	@Action(value = "createStore", results = { @Result(name = "success", type = "json") })
	public String create() {
		LOGGER.info(" Start action - 'createStore'");
		storeService.create(model);
		LOGGER.info(" End action - 'createStore'");
		return SUCCESS;
	}
	
	@Action(value = "editStore", results = { @Result(name = "success", type = "json") })
	public String edit() {
		LOGGER.info(" Start action - 'editStore'");
		LOGGER.info(" End action - 'editStore'");
		return SUCCESS;
	}

	@Action(value = "deleteStore", results = { @Result(name = "success", type = "json") })
	public String delete() {
		LOGGER.info(" Start action - 'deleteStore'");
		storeService.delete(model);
		LOGGER.info(" End action - 'deleteStore'");
		return SUCCESS;
	}

	@Action(value = "getStoreTypes", results = { @Result(name = "success", type = "json") })
	public String getStoreTypes() {
		LOGGER.info(" Start action - 'getStoreTypes'");
		LOGGER.info(" End action - 'getStoreTypes'");
		return SUCCESS;
	}

	@Action(value = "getStoreStatuses", results = { @Result(name = "success", type = "json") })
	public String getStoreStatuses() {
		LOGGER.info(" Start action - 'getStoreStatuses'");
		LOGGER.info(" End action - 'getStoreStatuses'");
		return SUCCESS;
	}

	public StoreModel getModel() {
		return model;
	}
}
